package com.sduept.nwld.dataserver.manager.statistics;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.sduept.bigdata.fault.special.maintenance.manager.MaintenancePersistManager;
import com.sduept.bigdata.fault.special.maintenance.manager.MaintenancePersistManager.DataCache;
import com.sduept.bigdata.fault.special.maintenance.manager.MaintenancePersistManager.HandlerChain;
import com.sduept.bigdata.fault.special.maintenance.manager.MaintenancePersistManager.MaintenanceEquWrapper;
import com.sduept.bigdata.fault.special.maintenance.manager.MaintenancePersistManager.SingleStationContext;
import com.sduept.bigdata.fault.special.maintenance.manager.MaintenanceQueryManager;
import com.sduept.bigdata.special.maintenance.model.MaintenanceStation;
import com.sduept.bigdata.special.maintenance.model.MaintenanceStationBreaker;
import com.sduept.core.dao.SuperDAO;

/**
 *特维关联检修票定时任务
 */
 @Service
public class SpecialMaintainOverhaulSchedule {

    @Inject
	private SuperDAO dao;
    @Inject
    private MaintenanceQueryManager mqm;
    @Inject
    private MaintenancePersistManager mpm;
    


    /**
     *	 每天执行的任务，每天5点执行
     */
    @Scheduled(cron = "0 0 5  * * ?")
    public void dayExecute() {
       //TODO  当年特维设备里 没关联到检修票的 都要去刷一遍  如果没关联到检修票但有完成时间 查到对应检修票以后 设置
    	execute();
    }
    
    
    /**
     * 将没关联到检修票的查出来  先将完成时间都设置为null- 然后走处理链更新一系列字段  最后根据是否关联上检修票来判断是否更新该特维设备
     */
    private void execute() {
    	int year = getNowYear();
    	List<MaintenanceStation>  maintenanceStations = mqm.getMaintenanceStationByEmptyOverhaulId(year);//更新当前年份的特维设备
    	for (MaintenanceStation ms : maintenanceStations) {
    		try {
    			if(ms.getVoltage()!=500) {
    				continue;
    			}
    			ms.setFinishedTime(null);
				updateMaintenanceStation(ms);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    }
    
    /**
	 * 更新（保存）指定的特维厂站
	 * @param ms
	 * @throws Exception
	 */
	public void updateMaintenanceStation(MaintenanceStation ms) throws Exception {
		dao.delete(MaintenanceStationBreaker.class, "pid=? ",new Object[] {ms.getId()});
		HandlerChain fc = mpm.buildHandlerChain();
		DataCache dataCache = mpm.new DataCache(ms.getYear(),ms.getBlankId());
		SingleStationContext context = mpm.new SingleStationContext(ms.getYear(),dataCache,ms);
		fc.doHandler(context, fc);
		persist(context.getWrapperList());
	}
    
    
	/**
	 * 持久化特维信息
	 * @param wrapperList
	 */
	private void persist(List<MaintenanceEquWrapper> wrapperList){
		for(MaintenanceEquWrapper wrapper:wrapperList){
			MaintenanceStation ms = wrapper.getMaintenceEqu();
			if(StringUtils.isNotBlank(ms.getOverhaulId())&&StringUtils.isNotBlank(ms.getFinishedTime().toString())) {
				dao.createOrUpdate(ms, MaintenanceStation.class);
			}
			try {
				dao.persistentBatchUpdate(MaintenanceStationBreaker.class, wrapper.getMaintenceEquBreakers());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
    
    
    
    
    /**
     * 获取当前年份
     * @return
     */
    private int getNowYear() {
		Calendar date = Calendar.getInstance();
		String year = String.valueOf(date.get(Calendar.YEAR));
		return Integer.parseInt(year);
	}
   
}
